<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>新增退货单</title>
    <link rel="stylesheet" href="../../../../../public/dist/css/layui.css" />
    <link rel="stylesheet" href="../../../../../css/twocss/commit.css" />
    <link rel="stylesheet" href="../../../../../css/twocss/H/processing.css" />
</head>

<body>
    <div class="select common">
        <form class="layui-form" action="">
            <div class="select-title" style="height:40px;line-height:40px;">
                基本信息
            </div>
            <div class="basis-h">
                <div class="mess-pronew">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width:95px;">退货日期:</label
                >
                <div class="layui-input-inline">
                  <input type="text" class="layui-input" id="temis" />
                </div>
              </div>
            </div>

            <div class="layui-form-item">
              <div class="layui-inline ">
                <label class="layui-form-label" style="width:95px;"
                  >退货原因:</label
                >
                <div class="layui-input-inline ">
                  <input type="text" class="layui-input  whys " />
                </div>
              </div>
            </div>
          </div>

          <div class="mess-pronew">
            <div class="layui-form-item">
              <div class="layui-inline">
                <label class="layui-form-label" style="width:95px;"
                  >退货数量(袋):</label
                >
                <div class="layui-input-inline">
                  <input type="text" class="layui-input   weight" />
                </div>
              </div>
            </div>

            <div class="layui-form-item">
              <div class="layui-inline">
                <label class="layui-form-label" style="width:95px;"
                  >退货方式:</label
                >
                <div class="layui-input-inline">
                  <input type="text" class="layui-input   measure" />
                </div>
              </div>
            </div>
          </div>
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label" style="width:95px;"
                >负责人:</label
              >
              <div class="layui-input-inline">
                <select lay-filter="heads" class="headpeople">
                  <option value="">---请选择---</option>
                </select>
              </div>
            </div>
          </div>
        </div>
        <div
          class="select-title"
          style="height:30px;line-height:30px;margin-top: 10px;border-bottom-color:#c9c9c9;"
        >
          退货原辅料
        </div>
      </form>

      <table class="layui-hide" id="testretrue" lay-filter="testdome"></table>

      <div
        class="layui-btn-group"
        style="width: 100%;display: flex;justify-content: center;margin-top: 10px;"
      >
        <button class="layui-btn  saves">
          <i class="layui-icon">&#xe674;</i>保存
        </button>
        <button class="layui-btn huifan">
          <i class="layui-icon">&#xe65c;</i>返回
        </button>
      </div>
    </div>
  </body>

  <script src="../../../../../js/common/jquery.min.js"></script>
  <script src="../../../../../js/common/api.js"></script>
  <script src="../../../../../public/dist/layui.js" charset="utf-8"></script>
  <script src="../../../../../js/common/template-web.js"></script>
</html>
<script>
  layui.use(["table", "layer", "form", "laydate", "util"], function() {
    var $ = layui.jquery,
      table = layui.table,
      layer = layui.layer,
      laydate = layui.laydate,
      form = layui.form,
      projectobj = {},
      util = layui.util;

    //时间选择
    laydate.render({
      elem: "#temis",
      show: true,
      done: function(value) {
        var batch = value;
        projectobj.orderdate = batch + " " + "00:00:00";
      }
    });

    //负责人添加
    noAjax("user/getUser", {}, "GET")
      .done(function(resp) {
        $(".headpeople").append(new Option(resp.realName, resp.realName)); // 下拉菜单里添加元素
        layui.form.render("select");
      })
      .fail(function(err) {
        console.log(err);
      });

    //负责人的获取
    form.on("select(heads)", function(data) {
      projectobj.person = data.value;
      return;
    });

    var tableIns = table.render({
      elem: "#testretrue",
      url: base + "ordSale/getReturnSaleVo",
      method: "GET",
      where: {},
      headers: {
        Authorization: "Bearer" + " " + sessions
      },
      request: {
        pageName: "currentPage",
        limitName: "pageSize"
      },

      limits: [10, 20],
      parseData: function(res) {
        //res 即为原始返回的数据
        console.log(res);
        return {
          code: res.code, //解析接口状态
          msg: res.message, //解析提示文本
          totalNum: res.list.length, //解析数据长度
          lists: res.list //解析数据列表
        };
      },
      response: {
        statusName: "code", //数据状态的字段名称，默认：code
        statusCode: 10000, //成功的状态码，默认：0
        msgName: "msg", //状态信息的字段名称，默认：msg
        countName: "totalNum", //数据总数的字段名称，默认：count
        dataName: "lists" //数据列表的字段名称，默认：data
      },
      cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
      page: {
        //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
        layout: ["prev", "page", "next", "skip", "count"], //自定义分页布局
        //,curr: 5 //设定初始在第 5 页
        groups: 5, //只显示 1 个连续页码
        first: true, //不显示首页
        last: true, //不显示尾页
        prev: "上一页",
        next: "下一页",
        theme: "#c81623"
      },
      // height: 'full-20',//满高
      cols: [
        [
          {
            type: "radio",
            fixed: "left"
          },
          {
            field: "customername",
            title: "客户名称",
            minWidth: 120,
            align: "center"
          },
          {
            field: "customertype",
            title: "客户类型",
            minWidth: 150,
            align: "center",
            templet: function(d) {
              var num = null;
              if (d.customertype == "1") {
                num = "企业客户";
              }
              if (d.customertype == "2") {
                num = "个人客户";
              }
              return num;
            }
          },
          {
            field: "person",
            title: "负责人",
            minWidth: 120,
            sort: true,
            align: "center"
          },
          {
            field: "ordercode",
            title: "销售单号",
            minWidth: 170,
            sort: true,
            align: "center"
          },
          {
            field: "orderdate",
            title: "销售日期",
            minWidth: 130,
            sort: true,
            align: "center",
            templet: function(d) {
              var numv = null;
              if (d.orderdate == "" || d.orderdate == null) {
                numv = " ";
              } else {
                numv = d.orderdate = layui.util.toDateString(
                  d.orderdate,
                  "yyyy-MM-dd"
                );
              }
              return numv;
            }
          },
          {
            field: "productname",
            title: "产品名称",
            minWidth: 130,
            sort: true,
            align: "center"
          },
          {
            field: "batch",
            title: "批次",
            minWidth: 130,
            sort: true,
            align: "center"
          },
          {
            field: "num",
            title: "数量",
            minWidth: 130,
            sort: true,
            align: "center"
          }
        ]
      ]
    });

    //验证重量
    $(".weight").blur(function() {
      var reg = /^([0-9][1-9]*)+(.[0-9]{1,})?$/;
      if (!reg.test($(".weight").val())) {
        alerts("请正确输入数量!");
        $(this).val("");
        return;
      }
    });

    $(".saves").click(function() {
      if ($("#temis").val() == "") {
        alerts("请选择时间！");
        return;
      }
      if ($(".whys").val() == "") {
        alerts("请输入退货原因！");
        return;
      }
      if ($(".weight").val() == "") {
        alerts("请输退货数量！");
        return;
      }
      if ($(".headpeople").val() == "") {
        alerts("请选择负责人！");
        return;
      }
      if ($(".measure").val() == "") {
        alerts("请输入退货方式！");
        return;
      }
      var checkStatus = table.checkStatus("testretrue");
      var date = checkStatus.data;
      if (date.length <= 0) {
        alerts("请选择退货产品！");
        return;
      }
      $.each(date, function(index, item) {
        projectobj.supplytype = item.customertype;
        projectobj.supplyname = item.productname;
        projectobj.productname = item.productname;
        projectobj.batch = item.batch;
        projectobj.ordercode = item.ordercode;
      });
      projectobj.orderdate = $("#temis").val() + " " + "00:00:00";
      projectobj.reason = $(".whys").val();
      projectobj.num = $(".weight").val();
      projectobj.measure = $(".measure").val();
      projectobj.unit = "袋";
      console.log(projectobj);
      handleAjax("ordSale/addSaleOrdReturn", projectobj, "POST")
        .done(function(resp) {
          layer.msg("操作成功", {
            icon: 1,
            time: 1000
          });
          setTimeout(function() {
            window.location.href = "../productreturn.html";
          }, 1500);
          return;
        })
        .fail(function(err) {
          console.log(err);
        });
      return;
    });

    //返回
    $(".huifan").click(function() {
      window.location.href = "../productreturn.html";
    });
  });
</script>